package cn.edu.scnu.movies.service.impl;

import cn.edu.scnu.movies.entity.PageResult;
import cn.edu.scnu.movies.entity.QueryString;
import cn.edu.scnu.movies.entity.movies;
import cn.edu.scnu.movies.mapper.MoviesMapper;
import cn.edu.scnu.movies.service.MoviesService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class MoviesServiceImpl implements MoviesService {

    @Autowired
    private MoviesMapper moviesMapper;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;


    /**
     * 获取好评排行榜
     */
    @Override
    public PageResult<movies> getRatingRank(QueryString queryString) {
        String cacheKey = "rating_rank:page=" + queryString.getPage() + "&pageSize=" + queryString.getPageSize();

        // 1. 先从 Redis 中获取数据
        List<movies> moviesList = (List<movies>) redisTemplate.opsForValue().get(cacheKey);

        if (moviesList == null) {
            // 2. 如果 Redis 中没有数据，则设置分页参数并查询数据库
            PageHelper.startPage(queryString.getPage(), queryString.getPageSize());
            moviesList = moviesMapper.getRatingRank();

            // 3. 将查询结果写入 Redis 缓存，设置过期时间（例如 5 分钟）
            redisTemplate.opsForValue().set(cacheKey, moviesList, 5, TimeUnit.MINUTES);
        }

        // 4. 解析查询结果，并封装返回
        Page<movies> p = (Page<movies>) moviesList;
        return new PageResult<movies>(p.getTotal(), p.getResult());
    }

    /**
     * 获取本周排行耪
     */
    @Override
    public PageResult<movies> getWeekRank(QueryString queryString) {
        String cacheKey = "week_rank:page=" + queryString.getPage() + "&pageSize=" + queryString.getPageSize();

        // 1. 先从 Redis 中获取数据
        List<movies> moviesList = (List<movies>) redisTemplate.opsForValue().get(cacheKey);

        if (moviesList == null) {
            // 2. 如果 Redis 中没有数据，则设置分页参数并查询数据库
            PageHelper.startPage(queryString.getPage(), queryString.getPageSize());
            moviesList = moviesMapper.getWeekRank();

            // 3. 将查询结果写入 Redis 缓存，设置过期时间（例如 5 分钟）
            redisTemplate.opsForValue().set(cacheKey, moviesList, 5, TimeUnit.MINUTES);
        }

        // 4. 解析查询结果，并封装返回
        Page<movies> p = (Page<movies>) moviesList;
        return new PageResult<movies>(p.getTotal(), p.getResult());
    }

    /**
     * 获取本月排行耪
     */
    @Override
    public PageResult<movies> getMonthRank(QueryString queryString) {
        String cacheKey = "month_rank:page=" + queryString.getPage() + "&pageSize=" + queryString.getPageSize();

        // 1. 先从 Redis 中获取数据
        List<movies> moviesList = (List<movies>) redisTemplate.opsForValue().get(cacheKey);

        if (moviesList == null) {
            // 2. 如果 Redis 中没有数据，则设置分页参数并查询数据库
            PageHelper.startPage(queryString.getPage(), queryString.getPageSize());
            moviesList = moviesMapper.getMonthRank();

            // 3. 将查询结果写入 Redis 缓存，设置过期时间（例如 5 分钟）
            redisTemplate.opsForValue().set(cacheKey, moviesList, 5, TimeUnit.MINUTES);
        }

        // 4. 解析查询结果，并封装返回
        Page<movies> p = (Page<movies>) moviesList;
        return new PageResult<movies>(p.getTotal(), p.getResult());
    }
    @Override
    public PageResult<movies> getHotMovies(QueryString queryString) {
        String cacheKey = "hot_movies:page=" + queryString.getPage() + "&pageSize=" + queryString.getPageSize();

        // 1. 先从 Redis 中获取数据
        List<movies> moviesList = (List<movies>) redisTemplate.opsForValue().get(cacheKey);

        if (moviesList == null) {
            // 2. 如果 Redis 中没有数据，则设置分页参数并查询数据库
            PageHelper.startPage(queryString.getPage(), queryString.getPageSize());
            moviesList = moviesMapper.getHotMovies();

            // 3. 将查询结果写入 Redis 缓存，设置过期时间（例如 5 分钟）
            redisTemplate.opsForValue().set(cacheKey, moviesList, 5, TimeUnit.MINUTES);
        }

        // 4. 解析查询结果，并封装返回
        Page<movies> p = (Page<movies>) moviesList;
        return new PageResult<movies>(p.getTotal(), p.getResult());
    }
}
